package com.brillilab.starter.controller.aio.plans;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.comments.CommentsClassifyEnum;
import com.brillilab.domain.enums.comments.CommentsContentTypeEnum;
import com.brillilab.domain.po.comments.Comments;
import com.brillilab.domain.po.experiments.ExperimentComment;
import com.brillilab.domain.po.plans.PlanComment;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.comments.CommentsAddVo;
import com.brillilab.domain.vo.plans.PlanCommentAddVo;
import com.brillilab.domain.vo.plans.PlanCommentVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.comments.ICommentsService;
import com.brillilab.service.core.experiments.IExperimentCommentService;
import com.brillilab.service.core.plans.IPlanCommentService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.logic.comments.CommentsLogic;
import com.brillilab.service.logic.plans.PlanCommentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 工作计划评论
 *
 * @author LJ 2019-06-26
 */
@RestController
@RequestMapping("/api/planComment")
public class PlanCommentController {

	@Resource
	private UsersLogic usersLogic;
	@Resource
	private IPlanCommentService planCommentService;
	@Resource
	private PlanCommentLogic planCommentLogic;
	@Resource
	private IFileManageService fileManageService;
	@Resource
	private IExperimentCommentService experimentCommentService;
	@Resource
	private ICommentsService commentsService;
	@Resource
	private CommentsLogic commentsLogic;
	
	
	/**
	 * 
	 * 计划人获取评论数
	 *
	 * @return
	 */
	@Deprecated
	@GetMapping("/num")
	public ResponseVo commentNum(@RequestParam Long planMemberId, @RequestParam String startDate,
			@RequestParam String endDate, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		int commentNum = 0;
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		if (userInfo.getLabMemberId().equals(planMemberId)) {
			commentNum = planCommentService.getNoReadNum(planMemberId, startDate, endDate);
		}
		return ResponseVo.successMap("commentNum", commentNum);
	}

	/**
	 * 获取计划评论列表
	 *
	 * @return
	 */
	 @Deprecated
	@GetMapping("/list")
	public ResponseVo commentList(@RequestParam Long planMemberId, @RequestParam String startDate,
			@RequestParam String endDate, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<PlanCommentVo> planCommentList = planCommentLogic.getPlanCommentList(planMemberId, startDate, endDate);
		if (userInfo.getLabMemberId().equals(planMemberId) && !CollectionUtils.isEmpty(planCommentList)) {
			planCommentService.updateRead(planMemberId, startDate, endDate);
		}
		return ResponseVo.success(planCommentList);
	}

	/**
	 * 
	 * 计划人获取评论数
	 *
	 * @return
	 */
	@GetMapping("/unreadNum")
	public ResponseVo unreadNum(@RequestParam Long planMemberId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		int commentNum = 0;
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		if (userInfo.getLabMemberId().equals(planMemberId)) {
			commentNum = planCommentService.getUnreadNum(planMemberId);
		}
		return ResponseVo.successMap("commentNum", commentNum);
	}

	/**
	 * 获取评论分页
	 * 
	 * @param planMemberId
	 * @param pageNum
	 * @param pageSize
	 * @param sort   排序：0-降序（默认），1-升序
	 * @return
	 */
	@GetMapping("/page")
	public ResponseVo commentPage(@RequestParam Long planMemberId, 
			@RequestParam(defaultValue = "1") Integer  pageNum,
			@RequestParam(defaultValue = "10")Integer pageSize,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		PageVo<PlanCommentVo> page = planCommentLogic.commentPage(planMemberId, pageNum, pageSize);
		if (userInfo.getLabMemberId().equals(planMemberId) && !CollectionUtils.isEmpty(page.getContent())) {
			planCommentService.updateRead(planMemberId);
		}
		return ResponseVo.success(page);
	}

	/**
	 * 添加计划评论
	 *
	 * @return
	 */
	@PostMapping("/add")
	public ResponseVo commentAdd(@RequestBody PlanCommentAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		Long commentId = planCommentLogic.addPlanComment(addVo, userInfo);
		
		CommentsAddVo commentsAddVo = new CommentsAddVo();
		commentsAddVo.setClassify(CommentsClassifyEnum.PLAN.getValue());
		commentsAddVo.setClassifyId(addVo.getPlanMemberId());
		commentsAddVo.setContent(addVo.getContent());
		commentsAddVo.setContentType(CommentsContentTypeEnum.TEXT.getValue());
		
		if (!ArrayUtils.isEmpty(addVo.getCommentFileIds())) {
			Long fileId = addVo.getCommentFileIds()[0];
			FileManage fileManage = fileManageService.selectById(fileId);
			fileManage.setId(null);
			fileManage.setReferDataId(0L);
			fileManage.setAttachType(OssFileEnum.CommentFile.getKey());
			FileManage file = fileManageService.insert(fileManage);
			
			commentsAddVo.setFileId(file.getId());
			if(fileManage.getFileType().equals(0)) {
				commentsAddVo.setContentType(CommentsContentTypeEnum.FILE.getValue());
			}else if(fileManage.getFileType().equals(1)) {
				commentsAddVo.setContentType(CommentsContentTypeEnum.VOICE.getValue());
			}else if(fileManage.getFileType().equals(2)) {
				commentsAddVo.setContentType(CommentsContentTypeEnum.VIDEO.getValue());
			}else if(fileManage.getFileType().equals(3)) {
				commentsAddVo.setContentType(CommentsContentTypeEnum.PICTURE.getValue());
			}
		}
		commentsLogic.addComments(userInfo.getLabId(), userInfo.getLabMemberId(), commentsAddVo);
		
		return ResponseVo.successMap("commentId", commentId);
	}

	/**
	 * 删除计划评论
	 *
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo commentDelete(@RequestParam Long commentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		planCommentLogic.deletePlanComment(commentId);
		return ResponseVo.success();
	}
	
	/**
	 * 数据初始化
	 * @return
	 */
	@GetMapping("/init")
	public ResponseVo commentInit(@RequestParam String pw) {
		if(pw.equals("zxcvbnm")) {
			List<ExperimentComment> expList= experimentCommentService.selectAllList();
			for (ExperimentComment exp : expList) {
				Comments comments = new Comments();
				comments.setId(exp.getId());
				comments.setLabMemberId(exp.getLabMemberId());
				comments.setClassify(CommentsClassifyEnum.EXPERIMENT.getValue());
				comments.setClassifyId(exp.getExperimentId());
				comments.setContent(exp.getContent());
				comments.setType(exp.getType());
				comments.setContentType(CommentsContentTypeEnum.TEXT.getValue());
				comments.setCreateTime(exp.getCreateTime());
				comments.setUpdateTime(exp.getUpdateTime());
				
				if (StrUtil.isBlank(exp.getContent())) {
					List<FileManage>  fileList = fileManageService.selectList(OssFileEnum.CommentFile, exp.getId());
					if(CollUtil.isNotEmpty(fileList)) {
						FileManage fileManage = fileList.get(0);
						
						if(fileManage.getFileType().equals(0)) {
							comments.setContentType(CommentsContentTypeEnum.FILE.getValue());
						}else if(fileManage.getFileType().equals(1)) {
							comments.setContentType(CommentsContentTypeEnum.VOICE.getValue());
						}else if(fileManage.getFileType().equals(2)) {
							comments.setContentType(CommentsContentTypeEnum.VIDEO.getValue());
						}else if(fileManage.getFileType().equals(3)) {
							comments.setContentType(CommentsContentTypeEnum.PICTURE.getValue());
						}

						commentsService.save(comments);
						
						fileManage.setId(null);
						fileManage.setAttachType(OssFileEnum.CommentFile.getKey());
						fileManage.setReferDataId(comments.getId());
						fileManage.setCreateTime(new Date());
						fileManage.setUpdateTime(new Date());
						fileManageService.insert(fileManage);
					}else {
						commentsService.save(comments);
					}
				}else {
					commentsService.save(comments);
				}
			}
			
			
			
			List<PlanComment> planList= planCommentService.selectAllList();
			for (PlanComment plan : planList) {
				Comments comments = new Comments();
				comments.setLabMemberId(plan.getLabMemberId());
				comments.setClassify(CommentsClassifyEnum.PLAN.getValue());
				comments.setClassifyId(plan.getPlanMemberId());
				comments.setContent(plan.getContent());
				comments.setType(0);
				comments.setContentType(CommentsContentTypeEnum.TEXT.getValue());
				comments.setCreateTime(plan.getCreateTime());
				comments.setUpdateTime(plan.getUpdateTime());
				comments.setRemark(String.valueOf(plan.getPlanDate().getTime()));
				
				if (StrUtil.isBlank(plan.getContent())) {
					List<FileManage>  fileList = fileManageService.selectList(OssFileEnum.PlanCommentFile, plan.getId());
					if(CollUtil.isNotEmpty(fileList)) {
						FileManage fileManage = fileList.get(0);
						if(fileManage.getFileType().equals(0)) {
							comments.setContentType(CommentsContentTypeEnum.FILE.getValue());
						}else if(fileManage.getFileType().equals(1)) {
							comments.setContentType(CommentsContentTypeEnum.VOICE.getValue());
						}else if(fileManage.getFileType().equals(2)) {
							comments.setContentType(CommentsContentTypeEnum.VIDEO.getValue());
						}else if(fileManage.getFileType().equals(3)) {
							comments.setContentType(CommentsContentTypeEnum.PICTURE.getValue());
						}
						commentsService.save(comments);
						
						fileManage.setId(null);
						fileManage.setAttachType(OssFileEnum.CommentFile.getKey());
						fileManage.setReferDataId(comments.getId());
						fileManage.setCreateTime(new Date());
						fileManage.setUpdateTime(new Date());
						fileManageService.insert(fileManage);
						
					}else {
						commentsService.save(comments);
					}
				}else {
					commentsService.save(comments);
				}
			}
		}
		return ResponseVo.success();
	}

}
